package app.msy.cn.rxandroid.model;


import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import app.msy.cn.rxandroid.api.DotNetApi;
import app.msy.cn.rxandroid.bean.LoginResp;
import app.msy.cn.rxandroid.listener.OnLoginListener;
import okhttp3.Call;

public class LoginModelImpl implements ILoginModel {

    @Override
    public void login(String userName, String passWord, final OnLoginListener listener) {
        String url = DotNetApi.getLoginUrl(userName, passWord);
        OkHttpUtils
                .get()
                .url(url)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        listener.loginFailed();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        LoginResp resp = new Gson().fromJson(response, LoginResp.class);
                        listener.loginSuccess(resp);
                    }
                });
    }
}
